檢查10*10五子棋可以構成5個連為一線的位置。
1表示有放棋子,0表示沒有放旗子。
輸入說明
輸入10*10的資料
輸出說明
可構成5個連為一線的位置。例如
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 1 1 1
0 0 0 0 1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0
可以增加5個連為一線,以下圖表示。
0 0 0 0 0 0 x 0 1 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 x 1 1 1 1
0 0 0 0 1 0 1 0 0 0
0 0 0 x 0 0 x 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 x 1 1 1 1 x 0 0 0
0 0 0 1 0 0 0 0 0 0
其位置為
0 6
3 5
5 3
5 6
8 1
8 6
SampleInput:
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 1 1 1
0 0 0 0 1 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 1 0 0 0 0 0 0
Sample Output:
0 6
3 5
5 3
5 6
8 1
8 6
#include <stdio.h>
int main(){
int gobang[10][10];
int check[10][10]={0};
int temp=0;
int i,j;
for(i=0;i<10;i++){
for(j=0;j<10;j++){
scanf("%d",&gobang[i][j]);
//printf("%d ",gobang[i][j]);
if(gobang[i][j]==1){
check[i][j]=1;
}
}
}
for(i=0;i<10;i++){
for(j=0;j<10;j++){
if(check[i][j]==1 && i>=4){//up
temp=check[i][j]+check[i-1][j]+check[i-2][j]+check[i-3][j]+check[i-4][j];
if(temp==4){
if(check[i][j]==0)
check[i][j]=100;
else if(check[i-1][j]==0)
check[i-1][j]=100;
else if(check[i-2][j]==0)
check[i-2][j]=100;
else if(check[i-3][j]==0)
check[i-3][j]=100;
else if(check[i-4][j]==0)
check[i-4][j]=100;
}
temp=0;
}
if(check[i][j]==1 && j>=4){//left
temp=check[i][j]+check[i][j-1]+check[i][j-2]+check[i][j-3]+check[i][j-4];
if(temp==4){
if(check[i][j]==0)
check[i][j]=100;
else if(check[i][j-1]==0)
check[i][j-1]=100;
else if(check[i][j-2]==0)
check[i][j-2]=100;
else if(check[i][j-3]==0)
check[i][j-3]=100;
else if(check[i][j-4]==0)
check[i][j-4]=100;
}
temp=0;
}
if(check[i][j]==1 && j<=5){//right
temp=check[i][j]+check[i][j+1]+check[i][j+2]+check[i][j+3]+check[i][j+4];
if(temp==4){
if(check[i][j]==0)
check[i][j]=100;
else if(check[i][j+1]==0)
check[i][j+1]=100;
else if(check[i][j+2]==0)
check[i][j+2]=100;
else if(check[i][j+3]==0)
check[i][j+3]=100;
else if(check[i][j+4]==0)
check[i][j+4]=100;
}
temp=0;
}
/*if(check[i][j]==1 && i<=4){//down
temp=check[i][j]+check[i+1][j]+check[i+2][j]+check[i+3][j]+check[i+4][j];
if(temp==4){
if(check[i][j]==0)
check[i][j]=100;
else if(check[i+1][j]==0)
check[i+1][j]=100;
else if(check[i+2][j]==0)
check[i+2][j]=100;
else if(check[i+3][j]==0)
check[i+3][j]=100;
else if(check[i+4][j]==0)
check[i+4][j]=100;
}
temp=0;
}
if(check[i][j]==1 && i>=4 && j>=4){
temp=check[i][j]+check[i-1][j-1]+check[i-2][j-2]+check[i-3][j-3]+check[i-4][j-4];
if(temp==4){
if(check[i][j]==0)
check[i][j]=100;
else if(check[i-1][j-1]==0)
check[i-1][j-1]=100;
else if(check[i-2][j-2]==0)
check[i-2][j-2]=100;
else if(check[i-3][j-3]==0)
check[i-3][j-3]=100;
else if(check[i-4][j-4]==0)
check[i-4][j-4]=100;
}
temp=0;
}*/
}
}
for(i=0;i<10;i++){
for(j=0;j<10;j++){
if(check[i][j]==100){
printf("%d %d",i,j);
printf("\n");
}
}
}
}